package com.egghiro.framework.j2se.game.j2me;

import java.awt.event.KeyEvent;

public class J2MEKeyState {
    private static final int DEFAULT_KEY_STATE = 0;
    private static int keyState = DEFAULT_KEY_STATE;

    /**
     * 上键被按下
     * 
     * @param keyState
     * @return
     */
    public static boolean isKeyUpPressed(int keyState) {
        return (keyState & UP_PRESSED) != 0;
    }

    public static boolean isKeyUpPressed() {
        return isKeyUpPressed(keyState);
    }

    /**
     * 下键被按下
     * 
     * @param keyState
     * @return
     */
    public static boolean isKeyDownPressed(int keyState) {
        return (keyState & DOWN_PRESSED) != 0;
    }

    public static boolean isKeyDownPressed() {
        return isKeyDownPressed(keyState);
    }

    /**
     * 左键被按下
     * 
     * @param keyState
     * @return
     */
    public static boolean isKeyLeftPressed(int keyState) {
        return (keyState & LEFT_PRESSED) != 0;
    }

    public static boolean isKeyLeftPressed() {
        return isKeyLeftPressed(keyState);
    }

    /**
     * 右键被按下
     * 
     * @param keyState
     * @return
     */
    public static boolean isKeyRightPressed(int keyState) {
        return (keyState & RIGHT_PRESSED) != 0;
    }

    public static boolean isKeyRightPressed() {
        return isKeyRightPressed(keyState);
    }

    /**
     * 确定键被按下了
     * 
     * @param keyState
     * @return
     */
    public static boolean isKeyFirePressed(int keyState) {
        return (keyState & FIRE_PRESSED) != 0;
    }

    public static boolean isKeyFirePressed() {
        return isKeyFirePressed(keyState);
    }

    /**
     * GameA 被按下了 默认是1
     * 
     * @param keyState
     * @return
     */
    public static boolean isKeyGameAPressed(int keyState) {
        return (keyState & GAME_A_PRESSED) != 0;
    }

    public static boolean isKeyGameAPressed() {
        return isKeyGameAPressed(keyState);
    }

    /**
     * GameB被按下了 默认是3
     * 
     * @param keyState
     * @return
     */
    public static boolean isKeyGameBPressed(int keyState) {
        return (keyState & GAME_B_PRESSED) != 0;
    }

    public static boolean isKeyGameBPressed() {
        return isKeyGameBPressed(keyState);
    }

    /**
     * Game C 被按下了 默认是7
     * 
     * @param keyState
     * @return
     */
    public static boolean isKeyGameCPressed(int keyState) {
        return (keyState & GAME_C_PRESSED) != 0;
    }

    public static boolean isKeyGameCPressed() {
        return isKeyGameCPressed(keyState);
    }

    /**
     * GameD被按下了 默认是9
     * 
     * @param keyState
     * @return
     */
    public static boolean isKeyGameDPressed(int keyState) {
        return (keyState & GAME_D_PRESSED) != 0;
    }

    public static boolean isKeyGameDPressed() {
        return isKeyGameDPressed(keyState);
    }

    /**
     * GameE被按下了 默认是#号键
     * 
     * @param keyState
     * @return
     */
    public static boolean isKeyGameEPressed(int keyState) {
        return (keyState & GAME_E_PRESSED) != 0;
    }

    public static boolean isKeyGameEPressed() {
        return isKeyGameEPressed(keyState);
    }

    /**
     * 左软键被按下了
     * 
     * @param keyState
     * @return
     */
    public static boolean isKeyLeftSoftPressed(int keyState) {
        return (keyState & KEYSTATE_LEFT_SOFT) != 0;
    }

    public static boolean isKeyLeftSoftPressed() {
        return isKeyLeftSoftPressed(keyState);
    }

    /**
     * 右软键被按下了
     * 
     * @param keyState
     * @return
     */
    public static boolean isKeyRightSoftPressed(int keyState) {
        return (keyState & KEYSTATE_RIGHT_SOFT) != 0;
    }

    public static boolean isKeyRightSoftPressed() {
        return isKeyRightSoftPressed(keyState);
    }

    /**
     * 0被按下了
     * 
     * @param keyState
     * @return
     */
    public static boolean isKeyZeroPressed(int keyState) {
        return (keyState & KEYSTATE_0_PRESSED) != 0;
    }

    public static boolean isKeyZeroPressed() {
        return isKeyZeroPressed(keyState);
    }

    /**
     * 上键对应的KeyState
     */
    private static final int UP_PRESSED = 2;// 1;// 2;

    /**
     * 下键对应的KeyState
     */
    private static final int DOWN_PRESSED = 64;// 2;// 64;

    /**
     * 左键对应的KeyState
     */
    private static final int LEFT_PRESSED = 4;

    /**
     * 右键对应的KeyState
     */
    private static final int RIGHT_PRESSED = 32;// 8;// 32;

    /**
     * 确定键对应的keyState
     */
    private static final int FIRE_PRESSED = 256;// 16;// 256;

    /**
     * 游戏键A对应的KeyState
     */
    private static final int GAME_A_PRESSED = 512;// 32;// 512;

    /**
     * 游戏键B对应的KeyState
     */
    private static final int GAME_B_PRESSED = 1024;// 64;// 1024;

    /**
     * 游戏键C对应的KeyState
     */
    private static final int GAME_C_PRESSED = 2048;// 128;// 2048;

    /**
     * 游戏键D对应的KeyState
     */
    private static final int GAME_D_PRESSED = 4096;// 256;// 4096;
    /**
     * gameE的KeyState
     */
    private final static int GAME_E_PRESSED = 8192;// 512; // 8192;
    /**
     * 左软键的KeyState
     */
    private final static int KEYSTATE_LEFT_SOFT = 16384;// 1024;// 16384;
    /**
     * 右软键的KeyState
     */
    private final static int KEYSTATE_RIGHT_SOFT = 32768;// 2048;// 32768;
    /**
     * 按键0的keyState
     */
    private final static int KEYSTATE_0_PRESSED = 65536;

    public static void keyPressed(KeyEvent e) {
        switch (e.getKeyCode()) {
        case J2MEKey.UP:
            keyState = keyState | UP_PRESSED;
            break;
        // TODO THE OTHER KEY

        default:
            break;
        }
    }
}
